package cn.wenhaha.common.core.domain;

import cn.wenhaha.common.exception.ApiMSGException;
import com.jfinal.kit.StrKit;

import java.io.Serializable;

/**
 * Ajax请求工返回对象
 *
 ** @param <T>
 */
public class AjaxV1Result<T> implements Serializable{

	private static final long serialVersionUID = 1L;

	/** 成功的状态 */
	public static final String CODE_SUCCESS = "0";

	/** 失败的状态 */
	public static final String CODE_FAIL = "-1";

	/** 消息Code */
	private String code = CODE_SUCCESS;

	/**消息文本*/
	private String msg = "处理成功";

	/**需要返回的数据*/
	private T data;


	public AjaxV1Result() {
		super();
	}

	public AjaxV1Result(String code) {
		super();
		this.code = code;
	}

	public AjaxV1Result(String code, String msg) {
		super();
		this.code = code;
		this.msg = msg;
	}

	public AjaxV1Result(String code, String msg, T data) {
		super();
		this.code = code;
		this.msg = msg;
		this.data = data;
	}

	/**
	 * 获取实例信息
	 * @return AjaxRetPojo实体类
	 */
	public static AjaxV1Result<?> newInstance() {
		return new AjaxV1Result<Object>();
	}

	/**
	 * 获取成功的实例
	 *
	 * @return AjaxRetPojo实体类
	 */
	public static AjaxV1Result<String> newSuccess() {
		return newSuccess("处理成功");
	}

	/**
	 * 获取成功的实例
	 * @param msg 错误消息
	 * @return AjaxRetPojo实体类
	 */
	public static AjaxV1Result<String> newSuccess(String msg) {
		AjaxV1Result<String> ajaxRetPojo = new AjaxV1Result<String>();
		ajaxRetPojo.setCode(CODE_SUCCESS);
		ajaxRetPojo.setMsg(StrKit.defaultIfBlank(msg, "处理成功"));
		return ajaxRetPojo;
	}

	public static <F> AjaxV1Result<F> newSuccess(F data){
		return newSuccess("处理成功",data);
	}


	
	/**
	 * 获取成功的实例
	 * @param msg 错误消息
	 * @return AjaxRetPojo实体类
	 */
	public static <F> AjaxV1Result<F> newSuccess(String msg, F data) {
		AjaxV1Result<F> ajaxRetPojo = new AjaxV1Result<F>();
		ajaxRetPojo.setCode(CODE_SUCCESS);
		ajaxRetPojo.setData(data);
		ajaxRetPojo.setMsg(StrKit.defaultIfBlank(msg, "处理成功"));
		return ajaxRetPojo;
	}

	/**
	 * 获取失败的实例
	 *
	 * @return AjaxRetPojo实体类
	 */
	public static AjaxV1Result<?> newFail() {
		return newFail("处理失败");
	}

	public static  AjaxV1Result<?>  newFail(String msg, Object data) {
		AjaxV1Result<Object> ajaxRetPojo = new AjaxV1Result<Object>();
		ajaxRetPojo.setCode(CODE_FAIL);
		ajaxRetPojo.setData(data);
		ajaxRetPojo.setMsg(StrKit.defaultIfBlank(msg, "处理失败"));
		return ajaxRetPojo;
	}

	public static AjaxV1Result error(ApiMSGException apiMSGException)
	{
		return new AjaxV1Result(apiMSGException.getCode().toString(), apiMSGException.getMessage());
	}


	/**
	 * 获取失败的实例
	 * @param msg 错误消息
	 * @return AjaxRetPojo实体类
	 */
	public static AjaxV1Result<?> newFail(String msg) {
		return newFail(msg, null);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	/**
	 * 是否成功
	 * @return true 成功 false失败
	 */
	public boolean isSuccess() {
		return CODE_SUCCESS.equals(this.code);
	}
}
